home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / MATV.ZIP / MATV.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-11  |  4.1 KB  |  149 lines

  1.  
  2. // Matv - A Simple Matrix Class
  3.  
  4. // Version: 1.0
  5. // Author: Mark Von Tress, Ph.D.
  6. // Date: 10/11/92   
  7.  
  8. // Copyright(c) Mark Von Tress 1992
  9.  
  10.  
  11. // DISCLAIMER: THIS PROGRAM IS PROVIDED AS IS, WITHOUT ANY
  12. // WARRANTY, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  13. // TO FITNESS FOR A PARTICULAR PURPOSE. THE AUTHOR DISCLAIMS
  14. // ALL LIABILITY FOR DIRECT OR CONSEQUENTIAL DAMAGES RESULTING
  15. // FROM USE OF THIS PROGRAM.
  16.  
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <iostream.h>
  24. #include <fstream.h>
  25.  
  26. //////////////////////////////////////////////////// vector
  27. typedef enum bboolean { FFALSE, TTRUE } bboolean;
  28.  
  29. #ifndef SIGNATURE
  30. #define SIGNATURE 0x5a5a
  31. #endif
  32.  
  33. // uncomment the next lines to disable garbage checking
  34. //#ifndef NO_CHECKING
  35. //#define NO_CHECKING
  36. //#endif
  37.  
  38. //to use double precision, uncomment the next lines
  39. //#define USE_DOUBLES
  40.  
  41. #ifdef USE_DOUBLES
  42. #ifndef FLOAT
  43. #define FLOAT double
  44. #define LFLOAT long double
  45. #endif
  46. #else
  47. #ifndef FLOAT
  48. #define FLOAT float
  49. #define LFLOAT double
  50. #endif
  51. #endif
  52.  
  53.  
  54. class Vector
  55. {
  56. private :
  57.    FLOAT *c;           // vector of floats
  58.    int n;              // number of floats
  59.    bboolean CanDelete; // Gives permission to delete c - initially true
  60.    bboolean CanAlias;  // Can be aliased - initially true
  61.    long signature;     // for heap checking
  62.    FLOAT *SetupVectors(int n);
  63.    void PurgeVectors(void);
  64.  
  65. public :
  66.    Vector(void);
  67.    Vector(int n);
  68.    Vector(int n, FLOAT *x);
  69.    Vector(Vector &a);
  70.    Vector &operator = (Vector &a);
  71.    ~ Vector();
  72.  
  73.    FLOAT &operator() (int i);
  74.    int rows(void) { return n; }
  75.  
  76.    static FLOAT Nrerror(const char *errormsg);
  77.    void Garbage(const char *errormsg);
  78.  
  79.    friend ostream &operator << (ostream &s, Vector &v);
  80.    void release() { if (CanAlias == TTRUE) CanDelete = FFALSE; }
  81.    void CannotAlias() { CanAlias = FFALSE; }
  82.  
  83. };
  84.  
  85. /////////////////////////////////// matrix
  86.  
  87. class Matrix : public Vector
  88. {
  89. public :
  90.    int r, c;
  91.  
  92.    FLOAT &operator() (int i, int j);
  93.    Matrix(void) : Vector(1), r(1), c(1) { }
  94.    Matrix(int rr, int cc) : Vector(rr*cc), r(rr), c(cc) { }
  95.    Matrix(int rr, int cc, FLOAT *x) : Vector(rr*cc,x), r(rr), c(cc) { }
  96.    Matrix(Matrix &a) : Vector(a), r(a.r), c(a.c) { }
  97.    Matrix &operator = (Matrix &a);
  98.    ~ Matrix() { }
  99.    friend ostream &operator << (ostream &s, Matrix &m);
  100.    void show(char *str);
  101.  
  102.    friend Matrix operator + (Matrix &a, Matrix &b);
  103.    friend Matrix operator + (Matrix &a, FLOAT b);
  104.    friend Matrix operator + (FLOAT b, Matrix &a);
  105.    Matrix &operator += ( Matrix &a );
  106.    Matrix &operator += ( FLOAT b );
  107.  
  108.    friend Matrix operator - (Matrix &a, Matrix &b);
  109.    friend Matrix operator - (Matrix &a, FLOAT b);
  110.    friend Matrix operator - (FLOAT b, Matrix &a);
  111.    Matrix &operator -= ( Matrix &a );
  112.    Matrix &operator -= ( FLOAT b );
  113.    friend Matrix operator - (Matrix &a); // unary minus
  114.  
  115.    friend Matrix operator * (Matrix &a, Matrix &b);
  116.    friend Matrix operator * (Matrix &a, FLOAT b);
  117.    friend Matrix operator * (FLOAT b, Matrix &a);
  118.    Matrix &operator *= ( Matrix &a );
  119.    Matrix &operator *= ( FLOAT b );
  120.  
  121.    friend Matrix operator % (Matrix &a, Matrix &b); // emult
  122.    friend Matrix operator % (FLOAT b, Matrix &a);
  123.    friend Matrix operator % (Matrix &a, FLOAT b);
  124.    Matrix &operator %= ( Matrix &a );
  125.    Matrix &operator %= ( FLOAT b );
  126.  
  127.    friend Matrix operator / (Matrix &a, Matrix &b);
  128.    friend Matrix operator / (Matrix &a, FLOAT b);
  129.    friend Matrix operator / (FLOAT b, Matrix &a);
  130.    Matrix &operator /= ( Matrix &a );
  131.    Matrix &operator /= ( FLOAT b );
  132.  
  133.  
  134. };
  135.  
  136.  
  137. Matrix Tran(Matrix &a);                  // transpose
  138. Matrix Sweep(int k1, int k2, Matrix &a); // g2 sweep
  139. Matrix Inv(Matrix &a);                   // invert - G2 sweep
  140. Matrix Fill(int r, int cc, FLOAT d = 0);
  141. Matrix Ident(int n);
  142. Matrix Submat(Matrix &a, int lr, int lc, int r, int c);
  143. Matrix Ch( Matrix &a, Matrix &b);        // horizontal concatenation
  144. Matrix Cv( Matrix &a, Matrix &b);        // vertical concatentation
  145.  
  146. Matrix Reada(char *filename);
  147. void Writea(char *filename, Matrix &a, const char *filetitle = "A_Matrix");
  148.  
  149.